package org.example.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.annotation.Log;
import org.example.domain.po.ErpMenu;
import org.example.domain.po.ErpRole;
import org.example.domain.po.ErpUser;
import org.example.enums.BusinessType;
import org.example.service.ErpRoleService;
import org.example.service.impl.ErpMenuServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/menu")
@Api("/菜单管理")
public class ErpMenuController {
    @Autowired
    private ErpMenuServiceImpl erpMenuServiceImpl;
    
    @Autowired
    private ErpRoleService erpRoleService;
    
    @GetMapping("/findMenu")
    @ApiOperation("根据用户ID查询菜单(无树形)")
    public Result<List<ErpMenu>> getMenu(@RequestAttribute("user") ErpUser user) {
        ErpRole role = erpRoleService.getRoleByUserId(user.getId());
        if (StrUtil.equals(role.getName(), "租户")) {// 超级管理员
            return Result.success(erpMenuServiceImpl.findMenusAdmin());
        } else {
            return Result.success(erpMenuServiceImpl.findMenusByUserId(user.getId()));
        }
    }
    
//    @Log(operation = "菜单", businessType = BusinessType.OTHER, context = "查询菜单")
    @GetMapping("/findMenuTree")
    @ApiOperation("根据用户ID查询菜单(树形)")
    public Result<List<ErpMenu>> getMenuTree(@RequestAttribute("user") ErpUser user, String cname) {
        ErpRole role = erpRoleService.getRoleByUserId(user.getId());
        if (StrUtil.equals(role.getName(), "租户")) {// 租户
            return Result.success(erpMenuServiceImpl.findMenusAdminTree(0, cname));
        } else {
            List<ErpMenu> menuByUserIdTree = erpMenuServiceImpl.findMenuByUserIdTree(user.getId(), 0L);
            return Result.success(menuByUserIdTree);
        }
    }
    
    /**
     * 树形菜单
     */
    @GetMapping("/findMenuAll")
    @ApiOperation("根据用户ID查询菜单功能(树形)")
    public Result<List<ErpMenu>> findMenu(@RequestAttribute("user") ErpUser user, String cname) {
        ErpRole role = erpRoleService.getRoleByUserId(user.getId());
        if (StrUtil.equals(role.getName(), "租户")) {// 租户的左侧树形，不需要进行权限关联查询，直接显示menus里面的所有数据
            return Result.success(erpMenuServiceImpl.findMenusAdminTree(0, cname));
        } else {
            return Result.success(erpMenuServiceImpl.findMenuByUserIdTree(user.getId(), 0L));
        }
    }
    
    @GetMapping("/getMenusByUserId")
    @ApiOperation("/根据用户ID查询菜单（不包括顶级菜单）")
    public Result<List<ErpMenu>> getMenusByUserId(@RequestAttribute("user") ErpUser user) {
        return Result.success(erpMenuServiceImpl.getMenusByUserId(user.getId()));
    }
    
    @GetMapping("/getMenusByRoleId")
    @ApiOperation("/根据角色ID查询菜单（不包括顶级菜单）")
    public Result<List<ErpMenu>> findMenusByRoleId(@RequestAttribute("user") ErpUser user,Long roleId) {
        return Result.success(erpMenuServiceImpl.findMenusRoleId(roleId, user.getTenantId()));
    }
    
    
}
